package com.dlj.shirospringboot.config;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @author lijiedong
 * @version 1.0
 * @date 2020/12/3 15:36
 */
public class UserRealm extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
        // 给与授权，这个实际上可以从数据库中查询。
      authorizationInfo.addStringPermission("user:add,update");
        return authorizationInfo;

    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 用户名，密码，数据库中去取，这里直接使用硬编码
        String name="root";
        String password="123456";
        UsernamePasswordToken userToken= (UsernamePasswordToken) authenticationToken;
        if (!userToken.getUsername().equals(name)){
            return null; // 抛出UnknownAccountException
        }

        // 密码认证 shiro做
        return new SimpleAuthenticationInfo("",password,"");
    }
}
